package com.zs.controller;

import com.google.gson.Gson;
import com.zs.common.config.Global;
import com.zs.common.config.ResponseCode;
import com.zs.model.AdminEntity;
import com.zs.model.ResponseCodeEntity;
import com.zs.model.UserEntity;
import com.zs.service.EmployeeService;
import com.zs.service.UserService;

import org.apache.maven.model.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/auth")
public class LoginController  {
	@Resource
	private UserService userService;

	@Resource
	private Gson gson;
	//跳转到main页面
	
	@RequestMapping(value="/loginPage")
	public ModelAndView loginPage(HttpServletRequest request
	){
		ModelAndView mv=new ModelAndView("/views/auth/login.html");
		return mv;
	}

	@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public String login(HttpServletRequest request,
						@RequestParam(value = "username", required = true) String username,
						@RequestParam(value = "password", required = true) String password
	){
		System.out.println("login");
		AdminEntity adminEntity = new AdminEntity();
		adminEntity.setUsername(username);
		adminEntity.setPassword(password);
		UserEntity user=userService.checkLogin(username, password);
		if(user==null){
			return ResponseCode.LOGIN_FAIL;
		}
		HttpSession session = request.getSession();
		session.setAttribute(Global.CURRENTUSER, user);
		return gson.toJson(new ResponseCodeEntity(user));
	}

}
